package com.hjq.controller;

import com.hjq.anno.LogOperation;
import com.hjq.pojo.Clazz;
import com.hjq.pojo.queryparam.ClazzQueryParam;
import com.hjq.pojo.PageResult;
import com.hjq.pojo.Result;
import com.hjq.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 班级管理
 */
@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    ClazzService clazzService;

    /**
     * 班级信息分页查询
     * @return 信息
     */
    @GetMapping
    public Result findByPage(ClazzQueryParam clazzQueryParam) {
        log.info("查询班级信息,{}",clazzQueryParam);
        PageResult<Clazz> pageResult = clazzService.findByPage(clazzQueryParam);
        return Result.success(pageResult);
    }
    /**
     * 删除班级信息
     *
     * @param id id
     * @return result
     */
    @LogOperation
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id){
        log.info("删除班级信息,{}",id);
        clazzService.deleteById(Integer.parseInt(id));
        return Result.success();
    }
    /**
     * 根据id查询班级信息
     *
     * @param id id
     * @return result
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){
        log.info("根据id查询班级信息，id: {}", id);
        Clazz clazz = clazzService.findById(Integer.parseInt(id));
        return Result.success(clazz);
    }
    /**
     * 添加班级信息
     *
     * @param clazz clazz
     * @return result
     */
    @LogOperation
    @PostMapping
    public Result insert(@RequestBody Clazz clazz){
        log.info("添加班级信息：{}",clazz);
        clazzService.insert(clazz);
        return Result.success( );
    }

    /**
     * 更新班级
     * @param clazz clazz
     * @return  result
     */

    @LogOperation
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("更新班级信息：{}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    /**
     * 查询所有班级信息
     * @return 信息
     */
    @GetMapping("/list")
    public Result findAll(){
        List<Clazz> clazzList = clazzService.findAll();
        return Result.success(clazzList);
    }
}
